// pages/reward/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showIcon: true, //显示导航图标
		list: ['1', '2', '5', '10', '20', '50', '60', '80', '100'],
		teacherId: 0, //打赏教师id
		likeList: [], //赞赏列表
		totalCount: 0,
		number: 0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		this.setData({
			totalCount: options.number,
			teacherId: options.teacherId
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// var that = this;
		// var loginInfo = loginFn()
		// loginInfo.teacherId = this.data.teacherId
		// wx.request({
		//   url: urls.teacherlike,
		//   method: 'POST',
		//   data: loginInfo,
		//   success: function (res) {
		//     if (res.data.code == '0') {
		//       that.setData({
		//         totalCount: res.data.data.likeCount
		//       });
		//     }
		//   }
		// })
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	payFn: function(e) { //打赏
		console.log('打赏')
		console.log(e)
		var loginInfo = loginFn()
		loginInfo.orderType = '2' //打赏必须设置为2
		loginInfo.teacherId = this.data.teacherId //老师id
		loginInfo.price = this.data.list[e.currentTarget.dataset.idx] //打赏金额
		wx.request({
			url: urls.ordersign,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					wx.requestPayment({
						'timeStamp': res.data.data.timeStamp,
						'nonceStr': res.data.data.nonceStr,
						'package': res.data.data.package,
						'signType': 'MD5',
						'paySign': res.data.data.paySign,
						'success': function(res) {
							wx.showModal({
								title: '提示',
								content: '打赏成功',
								showCancel: false,
								confirmText: '确定',
								success: (res) => {
									wx.navigateBack()
								}
							})
						},
						'fail': function(res) {}
					})
				}
			}
		})
	},
	nopayFn: function(e) { //不打赏
		wx.navigateBack({
			delta: 1
		})
	}
})
